package flc.ast.activity;

import androidx.appcompat.app.AppCompatActivity;

import android.graphics.Color;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.stark.riddle.lib.model.bean.Riddle;
import com.stark.riddle.lib.model.bean.Twister;
import com.stark.riddle.lib.model.db.RiddleDaoHelperManager;

import java.util.ArrayList;
import java.util.List;

import flc.ast.BaseAc;
import flc.ast.R;
import flc.ast.databinding.ActivityRiddleBinding;
import flc.ast.utils.Content;
import stark.common.basic.inf.IRetCallback;
import stark.common.basic.utils.FastClickUtil;
import stark.common.basic.utils.RandomUtil;
import stark.common.basic.utils.SPUtil;
import stark.common.basic.view.container.StkRelativeLayout;

public class RiddleActivity extends BaseAc<ActivityRiddleBinding> {

    //猜谜语
    private int level = 0;
    private List<Riddle> listAll = new ArrayList<>();//全部数据
    private int rightNum;

    @Override
    protected int onCreate() {
        return R.layout.activity_riddle;
    }

    @Override
    protected void initView() {
        mDataBinding.ivRiddleBack.setOnClickListener(v -> onBackPressed());
        mDataBinding.rlRiddleOptionA.setOnClickListener(this);
        mDataBinding.rlRiddleOptionB.setOnClickListener(this);
        mDataBinding.rlRiddleOptionC.setOnClickListener(this);
        mDataBinding.rlRiddleOptionD.setOnClickListener(this);
        mDataBinding.ivRiddleTip.setOnClickListener(this);
    }

    @Override
    protected void onClickCallback(View v) {
        if (FastClickUtil.isFastClick()) return;
        switch (v.getId()) {
            case R.id.rlRiddleOptionA://选项A
                isRight(1, mDataBinding.rlRiddleOptionA, mDataBinding.tvRiddleOptionA);
                break;
            case R.id.rlRiddleOptionB://选项B
                isRight(2, mDataBinding.rlRiddleOptionB, mDataBinding.tvRiddleOptionB);
                break;
            case R.id.rlRiddleOptionC://选项C
                isRight(3, mDataBinding.rlRiddleOptionC, mDataBinding.tvRiddleOptionC);
                break;
            case R.id.rlRiddleOptionD://选项D
                isRight(4, mDataBinding.rlRiddleOptionD, mDataBinding.tvRiddleOptionD);
                break;
            case R.id.ivRiddleTip://提示
                tip();
                break;
        }
    }

    @Override
    protected void initData() {
        level = SPUtil.getInt(mContext, Content.RIDDLE_LEVEL, 1) - 1;//获取记录通过关卡数
        getLevel();
    }

    //获取关卡
    private void getLevel() {
        RiddleDaoHelperManager.getRiddleDbHelper().getByKind(Content.TWISTER_TYPE, 0, 80, new IRetCallback<List<Riddle>>() {
            @Override
            public void onResult(List<Riddle> result) {
                if (result != null && result.size() > 0) {
                    listAll.addAll(result);
                    setData();
                }
            }
        });
    }

    //设置游戏数据
    private void setData() {
        mDataBinding.tvRiddleLevel.setText((level + 1) + "/80");
        mDataBinding.tvRiddleProblem.setText(listAll.get(level).getRiddleDes());
        List<Riddle> list = RandomUtil.randomGetItems(listAll, listAll.get(level), 3);
        mDataBinding.tvRiddleOptionA.setText(list.get(0).getRiddleKey());
        mDataBinding.tvRiddleOptionB.setText(list.get(1).getRiddleKey());
        mDataBinding.tvRiddleOptionC.setText(list.get(2).getRiddleKey());
        mDataBinding.tvRiddleOptionD.setText(list.get(3).getRiddleKey());
        boolean isA = checkAnswer(list.get(0).getRiddleKey(), listAll.get(level).getRiddleKey());
        boolean isB = checkAnswer(list.get(1).getRiddleKey(), listAll.get(level).getRiddleKey());
        boolean isC = checkAnswer(list.get(2).getRiddleKey(), listAll.get(level).getRiddleKey());
        boolean isD = checkAnswer(list.get(3).getRiddleKey(), listAll.get(level).getRiddleKey());
        if (isA) {
            rightNum = 1;
        }
        if (isB) {
            rightNum = 2;
        }
        if (isC) {
            rightNum = 3;
        }
        if (isD) {
            rightNum = 4;
        }
    }

    //判断答案是否正确
    private boolean checkAnswer(String s1, String s2) {
        if (s1.equals(s2)) {
            return true;
        }
        return false;
    }

    //提示
    private void tip() {
        switch (rightNum) {
            case 1:
                isRight(rightNum, mDataBinding.rlRiddleOptionA, mDataBinding.tvRiddleOptionA);
                break;
            case 2:
                isRight(rightNum, mDataBinding.rlRiddleOptionB, mDataBinding.tvRiddleOptionB);
                break;
            case 3:
                isRight(rightNum, mDataBinding.rlRiddleOptionC, mDataBinding.tvRiddleOptionC);
                break;
            case 4:
                isRight(rightNum, mDataBinding.rlRiddleOptionD, mDataBinding.tvRiddleOptionD);
                break;
        }
    }

    //判断对错
    private void isRight(int kind, StkRelativeLayout viewBg, TextView text) {
        if (kind == rightNum) {
            viewBg.setBackgroundResource(R.drawable.dui1);
            new Handler().postDelayed(new Runnable() {
                @Override
                public void run() {
                    viewBg.setBackgroundResource(R.drawable.shape_option);
                    //判断是否需要解锁下一关
                    if (level < (listAll.size() - 1)) {//如果当前关卡小于总关卡数
                        int spLevel = SPUtil.getInt(mContext, Content.RIDDLE_LEVEL, 1);//获取记录通过关卡数
                        if (level == (spLevel - 1)) {//如果通过的是未解锁关卡则解锁下一关
                            SPUtil.putInt(mContext, Content.RIDDLE_LEVEL, spLevel + 1);
                        }
                        level++;
                    }
                    setData();
                }
            }, 500);
        } else {
            viewBg.setBackgroundResource(R.drawable.cuo1);
            text.setTextColor(Color.parseColor("#FFFFFF"));
            new Handler().postDelayed(new Runnable() {
                @Override
                public void run() {
                    viewBg.setBackgroundResource(R.drawable.shape_option);
                    text.setTextColor(Color.parseColor("#313131"));
                }
            }, 500);
        }
    }

}